package com.glory.dgcpcpda.activity.cpc;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.CpAssetsEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class CpAssetsActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.AssetsCode)
    FreeUI_ClearEditText AssetsCode;
    @BindView(R.id.etId)
    TextView etId;

    @BindView(R.id.Department)
    FreeUI_ClearEditText Department;
    @BindView(R.id.Procedures)
    FreeUI_ClearEditText Procedures;
    @BindView(R.id.Person)
    FreeUI_ClearEditText Person;
    @BindView(R.id.Position)
    FreeUI_ClearEditText Position;
    @BindView(R.id.Users)
    FreeUI_ClearEditText Users;
    @BindView(R.id.DateOfProd)
    FreeUI_ClearEditText DateOfProd;
    @BindView(R.id.SerialNum)
    FreeUI_ClearEditText SerialNum;
    @BindView(R.id.AssetOwner)
    FreeUI_ClearEditText AssetOwner;
    @BindView(R.id.AssetBrand)
    FreeUI_ClearEditText AssetBrand;
    @BindView(R.id.AssetModel)
    FreeUI_ClearEditText AssetModel;
    @BindView(R.id.FactoryCode)
    FreeUI_ClearEditText FactoryCode;
    @BindView(R.id.EquipmentCode)
    FreeUI_ClearEditText EquipmentCode;
    @BindView(R.id.ComputerName)
    FreeUI_ClearEditText ComputerName;
    @BindView(R.id.DomainUname)
    FreeUI_ClearEditText DomainUname;

    @BindView(R.id.SpEquipmentStatus)
    Spinner EquipmentStatus;
    @BindView(R.id.SpDeviceProp)
    Spinner DeviceProp;
    @BindView(R.id.SpSystemVersion)
    Spinner SystemVersion;

    @BindView(R.id.llMain)
    LinearLayout llMain;

    private String AsCode;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_assetsinfo;
    }

    @Override
    protected void initView() {
        tvTitle.setText("电脑资产");
        setMainLayout(llMain);

        initEdit(AssetsCode, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<CpAssetsEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<CpAssetsEntity>>
                    (new TypeToken<BaseResponseBean<CpAssetsEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<CpAssetsEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                        CpAssetsEntity body = response.body().getResponse().getBody();
                        if(body.getInventoryDtatus().equals("Y")){
                            tvTitle.setText("电脑资产-已盘点");
                        }else{
                            tvTitle.setText("电脑资产-未盘点");
                        }
                        SetEntity(body);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<CpAssetsEntity>> response) {
                    super.onError(response);
                    //loadError(response.getException(),"QueryCpInformation");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("AssetCode",getText(AssetsCode));
            NetUtils.requestPostCPC("CPC.QueryCpInformation","QueryCpInformation",this,map,callBack);
        });
        AssetsCode.requestFocus();
    }

    private void ClearEntity(){
        AsCode = "";
        AssetsCode.setText("");
        etId.setText("");
        Department.setText("");
        Procedures.setText("");
        Person.setText("");
        Position.setText("");
        Users.setText("");
        DateOfProd.setText("");
        SerialNum.setText("");
        AssetOwner.setText("");
        AssetBrand.setText("");
        AssetModel.setText("");
        FactoryCode.setText("");
        EquipmentCode.setText("");
        ComputerName.setText("");
        DomainUname.setText("");
        EquipmentStatus.setSelection(0);
        DeviceProp.setSelection(0);
        SystemVersion.setSelection(0);
        tvTitle.setText("电脑资产");
    }

    @Override
    protected void initData() {
        AsCode = "";
    }

    private void SetEntity(CpAssetsEntity cpAssetsEntity){
        AssetsCode.setText(cpAssetsEntity.getAssetCode());
        AsCode = cpAssetsEntity.getAssetCode();
        etId.setText(cpAssetsEntity.getId());
        Department.setText(cpAssetsEntity.getDepartment());
        Procedures.setText(cpAssetsEntity.getProcedures());
        Person.setText(cpAssetsEntity.getPerson());
        Position.setText(cpAssetsEntity.getPosition());
        Users.setText(cpAssetsEntity.getUsers());
        DateOfProd.setText(cpAssetsEntity.getDateOfProd());
        SerialNum.setText(cpAssetsEntity.getSerialNum());
        AssetOwner.setText(cpAssetsEntity.getAssetOwner());
        AssetBrand.setText(cpAssetsEntity.getAssetBrand());
        AssetModel.setText(cpAssetsEntity.getAssetModel());
        FactoryCode.setText(cpAssetsEntity.getFactoryCode());
        EquipmentCode.setText(cpAssetsEntity.getEquipmentCode());
        ComputerName.setText(cpAssetsEntity.getComputerName());
        DomainUname.setText(cpAssetsEntity.getDomainUname());

        switch (cpAssetsEntity.getEquipmentStatus()){
            case "在用":
                EquipmentStatus.setSelection(1);
                break;
            case "闲置":
                EquipmentStatus.setSelection(2);
                break;
            case "报废":
                EquipmentStatus.setSelection(3);
                break;
            default:
                EquipmentStatus.setSelection(0);
                break;
        }

        switch (cpAssetsEntity.getDeviceProp()){
            case "办公":
                DeviceProp.setSelection(1);
                break;
            case "机台":
                DeviceProp.setSelection(2);
                break;
            case "其他":
                DeviceProp.setSelection(3);
                break;
            default:
                DeviceProp.setSelection(0);
                break;
        }

        switch (cpAssetsEntity.getSystemVersion()){
            case "WindowsXp":
                SystemVersion.setSelection(1);
                break;
            case "Windows7":
                SystemVersion.setSelection(2);
                break;
            case "Windows10":
                SystemVersion.setSelection(3);
                break;
            case "Windows11":
                SystemVersion.setSelection(4);
                break;
            default:
                SystemVersion.setSelection(0);
                break;
        }
    }

    private CpAssetsEntity getEntity(){
        CpAssetsEntity cpAssetsEntity = new CpAssetsEntity();
        String etid = etId.getText().toString();
        cpAssetsEntity.setAssetCode(getText(AssetsCode));
        if(!etid.equals("")){
            cpAssetsEntity.setAssetCode(AsCode);
        }

        cpAssetsEntity.setId(etid);
        cpAssetsEntity.setDepartment(getText(Department));
        cpAssetsEntity.setProcedures(getText(Procedures));
        cpAssetsEntity.setPerson(getText(Person));
        cpAssetsEntity.setPosition(getText(Position));
        cpAssetsEntity.setUsers(getText(Users));
        cpAssetsEntity.setDateOfProd(getText(DateOfProd));
        cpAssetsEntity.setSerialNum(getText(SerialNum));
        cpAssetsEntity.setAssetOwner(getText(AssetOwner));
        cpAssetsEntity.setAssetBrand(getText(AssetBrand));
        cpAssetsEntity.setAssetModel(getText(AssetModel));
        cpAssetsEntity.setFactoryCode(getText(FactoryCode));
        cpAssetsEntity.setEquipmentCode(getText(EquipmentCode));
        cpAssetsEntity.setComputerName(getText(ComputerName));
        cpAssetsEntity.setDomainUname(getText(DomainUname));
        cpAssetsEntity.setEquipmentStatus(EquipmentStatus.getSelectedItem().toString());
        cpAssetsEntity.setDeviceProp(DeviceProp.getSelectedItem().toString());
        cpAssetsEntity.setSystemVersion(SystemVersion.getSelectedItem().toString());
        return cpAssetsEntity;
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                CpAssetsEntity entity = getEntity();

                if (entity.getAssetCode().equals("")){
                    showToast("资产编码不能为空",false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<JSONObject>>
                                (new TypeToken<BaseResponseBean<JSONObject>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showToast("操作成功",true);
                                    ClearEntity();
                                    AssetsCode.requestFocus();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                                super.onError(response);
                                loadError(response.getException(),"AddCpInformation");
                            }
                        };

                        Gson gson = new Gson();

                        JsonObject map = gson.fromJson(JSON.toJSONString(entity), JsonObject.class);

                        NetUtils.requestPostCPC("CPC.AddCpInformation","AddCpInformation",this,map,callBack);
                    }
                },"确定要提交吗？");
                break;
        }

    }

}
